package com.wzdigit.wms.wms.service.basic;

import com.wzdigit.mes.basic.api.BasicDataItemApi;
import com.wzdigit.mes.basic.api.BasicDataItemDetailApi;
import com.wzdigit.mes.basic.api.BasicGetCustomerApi;
import com.wzdigit.mes.basic.dto.CustomerDto;
import com.wzdigit.mes.basic.dto.DataItemDTO;
import com.wzdigit.mes.basic.dto.DataItemDetailDTO;
import com.wzdigit.wms.basic.client.request.basic.SetRootUserDataPermissionRequest;
import com.wzdigit.wms.basic.client.request.basic.SetUserDataPermissionRequest;
import com.wzdigit.wms.basic.client.response.Result;
import com.wzdigit.wms.basic.client.response.basic.UserDataPermissionResponse;
import com.wzdigit.wms.basic.enums.BaseIsEnabledEnum;
import com.wzdigit.wms.basic.enums.UserDataPermissionEnum;
import com.wzdigit.wms.basic.enums.WarehouseEnum;
import com.wzdigit.wms.basic.mapper.core.basic.UserDataPermissionMapper;
import com.wzdigit.wms.basic.mapper.core.basic.WarehouseMapper;
import com.wzdigit.wms.basic.util.HttpSessionUtil;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 用户授权服务类
 * @author yangdongjie
 * @UserDataPermissionService.java
 * @2021年1月18日下午4:02:50
 */
@Service
public class UserDataPermissionService {
	
	@Autowired
    private WarehouseMapper warehouseMapper;
	
	@Autowired
    private UserDataPermissionMapper userDataPermissionMapper;
	
	@Reference(version = "${dubbo.consumers.basic.customer.api:1.0.0}", check = false, timeout = 300000)
	private BasicGetCustomerApi customerApi;
	
	@Reference(version = "${dubbo.consumers.basic.data.item.detail.api:1.0.0}", check = false, timeout = 300000)
	private BasicDataItemDetailApi dataItemDetailApi;
	
	@Reference(version = "${dubbo.consumers.basic.data.item.api:1.0.0}", check = false, timeout = 300000)
	private BasicDataItemApi dataItemApi;

	public <T> Result<T> setUserDataPermission(SetRootUserDataPermissionRequest request,
			HttpServletRequest servletRequest) {
		String loginName = HttpSessionUtil.getLoginName(servletRequest);
		Date createTime = new Date();
		
		for (SetUserDataPermissionRequest one : request.getDataPerms()) {
			userDataPermissionMapper.deleteNot(request.getUserId(), one.getAttr(), one.getPermIds());
			for (Integer value : one.getPermIds()) {
				if (value > 0) {
					userDataPermissionMapper.insertOfUpdateOne(request.getUserId(), one.getAttr(), 
							value.toString(), BaseIsEnabledEnum.USE.code, loginName, createTime);
				}
			}
		}
		return new Result<>();
	}
	
	public List<UserDataPermissionResponse> getPermsByUserIdAndAttr(Integer userId, String attr) {
		int groupPermId = 0;
		List<UserDataPermissionResponse> response = new ArrayList<UserDataPermissionResponse>(0);
		switch (UserDataPermissionEnum.getEnumByName(attr)) {
		case WHS:	// 仓库权限
			for (WarehouseEnum warehouseEnum : WarehouseEnum.values()) {
				UserDataPermissionResponse one = new UserDataPermissionResponse();
				one.setPermId(--groupPermId);
				one.setIsGranted(false);
				one.setPermName(warehouseEnum.getEnumName());
				one.setChildPerms(warehouseMapper.selectPerms(userId, warehouseEnum.getEnumName()));
				setIsGranted(one);
				response.add(one);
			}
			break;
		case FAC:	// 工厂权限
			getDataItemPerms(groupPermId, attr, "FACTORY_DATA", userId, response);
			break;
		case OWN:	// 货主权限
			List<CustomerDto> customers = customerApi.getCustomer("货主");
			UserDataPermissionResponse one = new UserDataPermissionResponse();
			one.setPermId(--groupPermId);
			one.setIsGranted(false);
			one.setPermName("货主");
			if(customers != null && customers.size() > 0) {
				one.setChildPerms(userDataPermissionMapper.selectCustomerPerms(userId, customers));
			} else {
				one.setChildPerms(new ArrayList<>(0));
			}
			setIsGranted(one);
			response.add(one);
			break;
		case CTC:	// 成本中心
			getDataItemPerms(groupPermId, attr, "COST_DATA", userId, response);
			break;
		case WKC:	// 工作中心
			getDataItemPerms(groupPermId, attr, "WORK_DATA", userId, response);
			break;
		}
		return response;
    }
	
	/**
	 * 数据字典维护数据权限
	 * @param groupPermId
	 * @param attr
	 * @param parentCode
	 * @param userId
	 * @param response
	 */
	private void getDataItemPerms(int groupPermId, String attr, String parentCode, Integer userId,
			List<UserDataPermissionResponse> response) {
		List<DataItemDTO> dataItems = dataItemApi.getDataItemByParentCode(parentCode);
		for (DataItemDTO dataItem : dataItems) {
			UserDataPermissionResponse one = new UserDataPermissionResponse();
			one.setPermId(--groupPermId);
			one.setIsGranted(false);
			one.setPermName(dataItem.getItemName());
			List<DataItemDetailDTO> dataItemDetails = dataItemDetailApi.getDataItemDetailByCode(dataItem.getItemCode());
			if (dataItemDetails != null && dataItemDetails.size() > 0) {
				one.setChildPerms(userDataPermissionMapper.selectDateItemPerms(attr, userId, dataItemDetails));
			} else {
				one.setChildPerms(new ArrayList<>(0));
			}
			setIsGranted(one);
			response.add(one);
		}
	}
	
	/**
	 * 设置父级菜单是否勾选
	 * @param one
	 */
	private void setIsGranted(UserDataPermissionResponse one) {
		for (UserDataPermissionResponse child : one.getChildPerms()) {
			if (child.getIsGranted()) {
				one.setIsGranted(true);
				break;
			}
		}
	}
}
